import axios from "axios";
//网络封装请求
import {Notify, Toast} from 'vant';
import router from "../router";
export  function request(config) {
    const instance =axios.create({
        baseURL:'https://api.shop.eduwork.cn',
        timeout:50000
    });
    //请求拦截
    instance.interceptors.request.use(config=>{
        const token =window.localStorage.getItem('token');
        if(token){
            config.headers.Authorization = 'Bearer '+token;
        }
        return config
    },err=>{

    });
    //响应拦截
    instance.interceptors.response.use(res=>{
        return res.data?res.data:res;
    },err=>{

        //如果有需要授权，才可以访问的接口，同意去login授权
        if (err.response.status=='401'){
            Toast.fail('您尚未登录！');
            router.push({path:'/Login'})
        }
        //如果有错误，在这里面会处理，显示错误信息
        Notify(err.response.data.errors[Object.keys(err.response.data.errors)[0]][0])
    });
    return instance(config);
}